<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 13/03/2018
 * Time: 5:06 PM
 */

namespace app\customer\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use php_excel\libs\Excel;
use think\Db;

class Export extends Admin
{
    //客户资料导出
    public function export()
    {
        return ZBuilder::make("form")
            ->addFormItems([
                ['radio', 'sex', '性别', '', [1 => '全部', 2 => '男', 3 => '女'], 1],
                ['linkAge:6', 'netId', '所属网点', '', Db::name('netsite_record')->column('net_name', 'id'), '', url('getManagers'), 'manager_id'],
                ['select:6', 'manager_id', '客户经理', '', []],
                ['select', 'level_id', '客户分组', '', Db::name('customer_level')->column('name', 'id')]
            ])
            ->method('get')
            ->isAjax(false)
            ->setUrl(url('doExport'))
            ->fetch();
    }

    //导出客户资料
    public function doExport()
    {
        /*step1 获取筛选条件*/
        $a_where = [];
        //性别
        if (input("?get.sex") && input('get.sex/d') !== 1 && in_array(input('get.sex/d'), [2, 3])):
            $a_sex = [
                2 => '男',
                3 => '女'
            ];
            $a_where['a.sex'] = $a_sex[input('get.sex/d')];
        endif;
        //所属网点,根据其所属客户经理所在网点进行网点判断
        $a_managerIds = [];
        if (input("?get.netId")):
            //网点名称
            $s_netName = Db::name('netsite_record')->where('id', input('get.netId/d'))->value('net_name');
            if (!empty($s_netName)):
                $a_managerIds = Db::name('manager')->where('net_name', $s_netName)->column('id');
            endif;
        endif;
        //所属客户经理
        if (input("?get.manager_id")):
            $i_managerId = input('get.manager_id/d');
            if ($i_managerId):
                $a_managerIds = [$i_managerId];
            endif;
        endif;
        if (!empty($a_managerIds)):
            $a_where['a.manager_id'] = ['in', $a_managerIds];
        else:
            //选择了网点，但是该网点下无客户
            if (!empty($s_netName)):
                $this->error("无符合条件的客户资料");
            endif;
        endif;
        //所属客户分组
        if (input("?get.level_id")):
            $i_id = input("get.level_id/d");
            if (!empty($i_id)):
                $a_where['a.level_id'] = $i_id;
            endif;
        endif;

        /*step2 根据筛选条件查询客户数据*/
        $a_customers = Db::name('customer a')
            ->join('manager b', 'a.manager_id=b.id')
            ->join('customer_level c', 'a.level_id=c.id', 'LEFT')
            ->where($a_where)
            ->field("a.name,a.sex,a.cert_number,a.tel1,a.tel2,a.birthday,a.hobbies,a.trade,a.career,a.nature,b.name manager_name,b.net_name net_name,c.name level_name")
            ->select();
        /*step3 利用excel类进行导出*/
        if (!empty($a_customers)):
            $a_customers = array_map(function ($a_item) {
                return array_values($a_item);
            }, $a_customers);
            $o_excel = new Excel();
            //长度
            $i_len = count($a_customers[0]);
            $a_letters = range('A', chr(ord('A') + $i_len - 1));
            $a_headers = ['姓名', '性别', '身份证号', '电话1', '电话2', '生日', '爱好', '行业', '职业', '性格', '客户经理', '网点', '分组'];
            $o_excel->export($a_letters, $a_headers, $a_customers, '客户资料导出' . uniqid());
            exit;
        else:
            $this->error("无符合条件的客户资料");
        endif;

    }

    //获取网点id
    public function getManagers($netId = '')
    {
        //step1 根据网点Id获取网点名称
        $s_netName = Db::name('netsite_record')->where('id', intval($netId, 0))->value('net_name');
        //step2 根据网点名称获取对应的客户经理
        $a_managers = Db::name('manager')->where('net_name', $s_netName)->column('name', 'id');
        return json([
            'code' => 1,
            'msg' => '',
            'list' => format_linkage($a_managers)
        ]);
    }
}